package com.yourenbang.lingyun.transaction.web.controller.pay.withdraw;

import com.alibaba.fastjson.JSONObject;
import com.yourenbang.lingyun.constants.enums.pay.PayTypeEnum;
import com.yourenbang.lingyun.core.base.controller.BaseController;
import com.yourenbang.lingyun.core.base.interceptor.ExcludeInterceptor;
import com.yourenbang.lingyun.core.message.Request;
import com.yourenbang.lingyun.core.message.Result;
import com.yourenbang.lingyun.core.util.KLog;
import com.yourenbang.lingyun.core.util.StringUtils;
import com.yourenbang.lingyun.transaction.util.fusuilinggong.FuSuiLingGongUtil;
import com.yourenbang.lingyun.transaction.web.service.pay.withdraw.WithdrawService;
import com.yourenbang.transaction.api.model.dto.pay.WithDrawParamsDTO;
import com.yourenbang.transaction.api.model.dto.pay.WithDrawVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;

@Slf4j
@RestController
@RequestMapping("/transaction/fund")
@CrossOrigin("*")
public class WithdrawController extends BaseController {

    @Autowired
    private WithdrawService withdrawService;

    /**
     * 新凌云提现
     *
     * @see <a href="https://huoyan.kaolakuaishou.cn/project/97/interface/api/14627">火眼接口连接</a>
     */
    @ExcludeInterceptor
    @RequestMapping(value = "/wallet/withdraw/v2", method = RequestMethod.POST)
    public Result<WithDrawVO> walletWithdrawV2(@Validated @RequestBody Request<WithDrawParamsDTO> dto, BindingResult br) {
        KLog.info("【凌云提现】入参:{}", dto.toJSON());
        Result result = verifyParam(br);
        if (!result.isSuccess()) {
            return result;
        }
        return withdrawService.withdrawV2(dto.getData());
    }

    /**
     * 福穗灵工订单证据
     */
    @ExcludeInterceptor
    @RequestMapping(value = "/withdraw/fusuilinggong/{order}.xls", method = RequestMethod.GET)
    public void getFuSuiOrder(@PathVariable("order") String orderNo){
        KLog.info("【福穗灵工订单证据】入参:{}", orderNo);
        withdrawService.getFuSuiOrderEvidence(orderNo);
    }

    /**
     * 福穗灵工回调
     */
    @ExcludeInterceptor
    @PostMapping(value = "/withdraw/fusuilinggong/callback")
    public String fuSuiCallback(@RequestParam HashMap<String, String> params) {
        KLog.info("【福穗灵工回调】入参:{}", JSONObject.toJSONString(params));
        String msgMethod = params.get("msgMethod");
        //用工计划支付状态变更回调
        if (StringUtils.equals(msgMethod, FuSuiLingGongUtil.MSG_METHOD_PLAN_STATUS_CALLBACK)) {
            boolean noticeResult = withdrawService.fundPayNoticeBody(PayTypeEnum.FUSUILINGGONG.getCode(), params.get("bizContent"));
            return noticeResult ? FuSuiLingGongUtil.RESPONSE_SUCCESS : FuSuiLingGongUtil.RESPONSE_ERROR;
        }
        return FuSuiLingGongUtil.RESPONSE_SUCCESS;
    }
}
